package top.laoshuzi.designpatterns.base.created.prototype;

import java.util.HashMap;
import java.util.Map;


/**
 * 原型管理器
 */
public class PrototypeManager {

    private static Map<String, Prototype> map = new HashMap<String, Prototype>();

    private PrototypeManager() {

    }

    public synchronized static void registerPrototype(String prototypeId, Prototype prototype) {
        map.put(prototypeId, prototype);
    }

    public synchronized static void unregisterPrototype(String prototypeId) {
        map.remove(prototypeId);
    }

    public synchronized static Prototype getPrototype(String prototypeId) throws Exception {
        Prototype prototype = map.get(prototypeId);
        if (prototype == null) {
            throw new Exception("原型" + prototypeId + "未注册或已销毁");
        }
        return prototype.clone();
    }

}
